package dk.modified.reshary;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CommentView extends RelativeLayout {

	public static final int STATE_ACTIVATED = 1;
	public static final int STATE_DEACTIVATED = 0;
	public static final int STATE_RATE_UP_CLICKED = 1337;
	public static final int STATE_RATE_DOWN_CLICKED = 7331;
	
	private LinearLayout container_action;
	
	private TextView txt_name;
	private TextView txt_date;
	private TextView txt_rating;
	private TextView txt_comment;

	private ImageButton cmd_reply;
	private ImageButton cmd_rate_plus;
	private ImageButton cmd_rate_minus;
	
	private ImageView imageView;

	private View indentView;

	private Boolean isPlusClicked = false;
	private Boolean isMinusClicked = false;

	private buttonsClickedListener mButtonsClickedListener;



	public CommentView(Context context) {
		super(context);
		init();
	}

	public CommentView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
	}

	public CommentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		inflate(getContext(), R.layout.comment_design, this);

		container_action = (LinearLayout) findViewById(R.id.container_action);
		
		txt_name = (TextView) findViewById(R.id.txt_name);
		txt_date = (TextView) findViewById(R.id.txt_date);
		txt_rating = (TextView) findViewById(R.id.txt_rating);
		txt_comment = (TextView) findViewById(R.id.txt_comment);

		cmd_reply = (ImageButton) findViewById(R.id.cmd_reply);
		cmd_reply.setOnClickListener(onClick_cmd_reply);
		cmd_rate_plus = (ImageButton) findViewById(R.id.cmd_rate_plus);
		cmd_rate_plus.setOnClickListener(onClick_cmd_rate_plus);
		cmd_rate_minus = (ImageButton) findViewById(R.id.cmd_rate_minus);
		cmd_rate_minus.setOnClickListener(onClick_cmd_rate_minus);

		imageView = (ImageView) findViewById(R.id.imageView);

		indentView = (View) findViewById(R.id.indentView);

	}

	public ImageView getImageView() {
		return imageView;
	}

	public void setName(String name) {
		txt_name.setText(name);
	}

	public void setDate(String date) {
		txt_date.setText(date);
	}

	public void setRating(String rating) {
		txt_rating.setText(rating);
	}

	public void setComment(String comment) {
		txt_comment.setText(comment);
	}

	public void showChild() {
		cmd_reply.setVisibility(View.GONE);
		indentView.setVisibility(View.VISIBLE);
	}

	public void showParent() {
		cmd_reply.setVisibility(View.VISIBLE);
		indentView.setVisibility(View.GONE);
	}
	
	public void showRateButtons(){
		cmd_rate_minus.setVisibility(View.VISIBLE);
		cmd_rate_plus.setVisibility(View.VISIBLE);
	}

	public void hideRateButtons(){
		cmd_rate_minus.setVisibility(View.GONE);
		cmd_rate_plus.setVisibility(View.GONE);
	}

	
	public void hideActionButtons(){
		container_action.setVisibility(View.GONE);
	}
	
	public void showActionButtons(){
		container_action.setVisibility(View.GONE);
	}
	
	private OnClickListener onClick_cmd_reply = new OnClickListener() {

		@Override
		public void onClick(View v) {
			if (mButtonsClickedListener != null)
				mButtonsClickedListener.replyClicked(null,0);
		}
	};

	private OnClickListener onClick_cmd_rate_plus = new OnClickListener() {

		@Override
		public void onClick(View v) {
//			int rating = Integer.parseInt(txt_rating.getText().toString());

			if (isMinusClicked) {
				clearRateDown();
//				rating += 1;
			}

			if (isPlusClicked) {
				clearRateUp();
//				txt_rating.setText(String.valueOf(rating - 1));
				if (mButtonsClickedListener != null)
					mButtonsClickedListener.rateUpClicked(STATE_DEACTIVATED, null);

			} else {
				isPlusClicked = true;
				setRateUpPressed();
//				txt_rating.setText(String.valueOf(rating + 1));
				if (mButtonsClickedListener != null)
					mButtonsClickedListener.rateUpClicked(STATE_ACTIVATED, null);
			}

		}
	};

	private OnClickListener onClick_cmd_rate_minus = new OnClickListener() {

		@Override
		public void onClick(View v) {
//			int rating = Integer.parseInt(txt_rating.getText().toString());

			if (isPlusClicked) {
				isPlusClicked = false;
				clearRateUp();
//				rating -= 1;
			}

			if (isMinusClicked) {
				isMinusClicked = false;
				clearRateDown();
//				txt_rating.setText(String.valueOf(rating + 1));
				if (mButtonsClickedListener != null)
					mButtonsClickedListener.rateDownClicked(STATE_DEACTIVATED, null);

			} else {
				setRateDownPressed();
//				txt_rating.setText(String.valueOf(rating - 1));
				if (mButtonsClickedListener != null)
					mButtonsClickedListener.rateDownClicked(STATE_ACTIVATED, null);
			}
		}
	};

	public void setOnButtonsClickedListener(buttonsClickedListener listener) {
		mButtonsClickedListener = listener;
	}
	
	
	public void clearRateDown(){
		isMinusClicked = false;
		cmd_rate_minus.setImageResource(R.drawable.ic_action_vote_down);
	}
	
	public void clearRateUp(){
		isPlusClicked = false;
		cmd_rate_plus.setImageResource(R.drawable.ic_action_vote_up);
	}
	
	public void clearRates(){
		cmd_rate_minus.setImageResource(R.drawable.ic_action_vote_down);
		cmd_rate_plus.setImageResource(R.drawable.ic_action_vote_up);
	}
	
	public void setRateDownPressed(){
		isPlusClicked = false;
		isMinusClicked = true;
		cmd_rate_plus.setImageResource(R.drawable.ic_action_vote_up);
		cmd_rate_minus.setImageResource(R.drawable.ic_action_vote_down_pressed);
	}
	
	public void setRateUpPressed(){
		isPlusClicked = true;
		isMinusClicked = false;
		cmd_rate_plus.setImageResource(R.drawable.ic_action_vote_up_pressed);
		cmd_rate_minus.setImageResource(R.drawable.ic_action_vote_down);
	}

}
